﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;

namespace IronOne.SalesOne.SalesOneWeb.Structure
{
    public partial class AddUnit : SOSecurityPage
    {
        private String action = "Create";
        private int successDel;
        private int successUpd;
        int flag;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            txtEffectiveFrom.Text = System.DateTime.Today.ToShortDateString();
            txtEffectiveTo.Text = System.DateTime.Today.ToShortDateString();

            //ValidateUserCredentials(Request.Url.LocalPath);


            

                ValidateUserCredentials(Request.Url.LocalPath);


                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    action = Request.QueryString["a"].ToString();

                }

                //if (!Page.IsPostBack)
                // {
                if (action == "v")
                {
                    LblHeader.Text = "View Unit";

                    if (!Page.IsPostBack)
                    {
                        this.View();
                    }

                }
                else if (action == "Create")
                {
                    // btnCancel.Visible = true;
                    LblHeader.Text = "Create new Unit";
                    if (!Page.IsPostBack)
                    {
                        this.Create();
                    }
                }

                else if (action == "u")
                {
                    LblHeader.Text = "Update Unit";

                    if (!Page.IsPostBack)
                    {
                        this.Update();
                    }



                }
            }
        }



        protected void BtnDone_Click(object sender, EventArgs e)
        {
            if (BtnDone.Text == "Back")
            {
                Response.Redirect("/Structure/ManageUnits.aspx");

            }
            else if (BtnDone.Text == "Cancel")
            {
                Session.Remove("unitId");
                Session.Remove("BID");
                Response.Redirect("~/Structure/ManageUnits.aspx");
            }
            else if (!String.IsNullOrEmpty(Request.QueryString["a"]))
            {
                StructureFacade unitFacade = new StructureFacade();
                int unitId = Convert.ToInt32(Session["unitId"].ToString());
                SalesOneUnit unit = unitFacade.GetUnit(unitId);
                Branch br = new Branch();
                br = unitFacade.GetBranch(Convert.ToInt32(drpBranch.SelectedValue));

                unit.BranchId = (Convert.ToInt32(drpBranch.SelectedValue));
                unit.Code = txtCode.Text;
                unit.Name = txtName.Text;

                DateTime dFrom = Convert.ToDateTime(txtEffectiveFrom.Text);
                DateTime dTo = Convert.ToDateTime(txtEffectiveTo.Text);

                unit.From = txtEffectiveFrom.Text;
                unit.To = txtEffectiveTo.Text;

                // unitFacade.UpdateUnit(unit);
                


                if ((dFrom < Convert.ToDateTime(br.EffectiveFrom.ToString()) || dTo > Convert.ToDateTime(br.EffectiveTo.ToString())))
                {
                    //lblResult.Visible = true;
                    //lblResult.Text = "Unit's effective period must be within the effective period of the relavant branch";
                    MassageSummery("IIS508");


                    flag = 1;
                }


                if (flag != 1)
                {
                    int i = unitFacade.UpdateUnit(unit);

                    if (i == 1)
                    {
                        Session["sccUnit"] = "Successfully Updated";
                    }
                    else
                    {
                        Session["failUnit"] = "Fail to Update";
                    }
                }

                //Redo the removing of these sessions and check again.
                if (flag != 1)
                {
                    Session.Remove("unitId");
                    Session.Remove("BID");
                }

            }
            else
            {
                StructureFacade unitFacade = new StructureFacade();
                SalesOneUnit unit = new SalesOneUnit();
                Branch br = new Branch();

                br = unitFacade.GetBranch(Convert.ToInt32(drpBranch.SelectedValue));


                unit.BranchId = (Convert.ToInt32(drpBranch.SelectedValue));
                unit.Code = txtCode.Text;
                unit.Name = txtName.Text;

                DateTime dFrom = Convert.ToDateTime(txtEffectiveFrom.Text);
                DateTime dTo = Convert.ToDateTime(txtEffectiveTo.Text);



                unit.From = txtEffectiveFrom.Text;
                unit.To = txtEffectiveTo.Text;



                try
                {
                    if (Convert.ToDateTime(txtEffectiveFrom.Text) < DateTime.Today)
                    {
                        //lblResult.Visible = true;
                        //lblResult.Text = "Effective from date must a future date";
                        //lblResult.ForeColor = System.Drawing.Color.Red;
                        MassageSummery("IIS507");

                        flag = 1;
                    }
                    else if ((dFrom < Convert.ToDateTime(br.EffectiveFrom.ToString()) || dTo > Convert.ToDateTime(br.EffectiveTo.ToString())))
                    {
                        //lblResult.Visible = true;
                        //lblResult.Text = "Unit's effective period must be within the effective period of the relavant branch";

                        MassageSummery("IIS508");
                        
                        flag = 1;
                    }
                    else
                    {
                        unitFacade.InsertUnit(unit);
                        Session["sccUnitIn"] = "Successfully Inserted";
                    }
                }
                catch (Exception)
                {
                    Session["failUnitIn"] = "Fail to insert";
                }
            }
            if (flag != 1)
            {
                Response.Redirect("~/Structure/ManageUnits.aspx");
            }
        }



        protected void drpGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDropDown("Branch");
        }

        public void FillDropDown(string type)
        {
            StructureFacade fac = new StructureFacade();

            switch (type)
            {
                case "Group":
                    List<SalesOneGroup> grp = fac.GetGroups("GroupId ASC");
                    SalesOneGroup grsel = new SalesOneGroup();
                    grsel.GroupId = -1;
                    grsel.Name = "Select...";
                    grp.Insert(0, grsel);
                    drpGroup.DataSource = grp;
                    drpGroup.DataTextField = "Name";
                    drpGroup.DataValueField = "GroupId";
                    drpGroup.DataBind();
                    break;

                case "Branch":
                    if (drpGroup.SelectedValue != "-1")
                    {
                        List<Branch> br = fac.GetBranchesForGroup(Convert.ToInt32(drpGroup.SelectedValue));
                        drpBranch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select...";
                        br.Insert(0, brSel);
                        drpBranch.DataTextField = "BranchName";
                        drpBranch.DataValueField = "BranchId";
                        drpBranch.DataBind();
                    }
                    break;

                case "BranchOrg":
                    List<Branch> grp1 = fac.GetBranches("BranchId ASC");
                    Branch br1 = new Branch();

                    br1.BranchId = -1;
                    br1.BranchName = "Select...";
                    grp1.Insert(0, br1);
                    drpBranch.DataSource = grp1;
                    drpBranch.DataTextField = "BranchName";
                    drpBranch.DataValueField = "BranchId";

                    drpBranch.DataBind();

                    break;

                default:
                    break;
            }
        }

        public void Create()
        {

            StructureFacade unitFecade = new StructureFacade();
            SalesOneGroup sog = new SalesOneGroup();
            Branch br = new Branch();
            LblHeader.Text = "Create Unit";
            BtnDone.Text = "Create";


            

           
            //if (!IsPostBack)
            //{
            if (Page.Request.QueryString["branchid_c"] != null && Page.Request.QueryString["unitid_c"] != null)
            {
                string branchstr = Page.Request.Params["branchid_c"].ToString();
                
                int branchID = int.Parse(branchstr);

                string unitStr = Page.Request.QueryString["unitid_c"];
                Session["unitId"] = unitStr;
                int unitID = int.Parse(unitStr);
            FillDropDown("Group");
            FillDropDown("BranchOrg");
            
            br = unitFecade.GetBranch(branchID);
            int groupid = br.GroupId;
            sog = unitFecade.GetGroup(groupid);
            drpGroup.SelectedValue = sog.GroupId.ToString();
            drpBranch.SelectedValue = br.BranchId.ToString();

            }
            if (Page.Request.Params["grp_id"] != null && Page.Request.Params["brch_id"].ToString()!=null)
            {
                FillDropDown("Group");
                FillDropDown("BranchOrg");

               
                drpGroup.SelectedValue = Page.Request.Params["grp_id"].ToString();
                drpBranch.SelectedValue = Page.Request.Params["brch_id"].ToString();
            }

            if (!IsPostBack)
            {
                FillDropDown("Group");
            }
            //}

        }

        public void View()
        {
            //btnCancel.Visible = true;
            BtnDone.Visible = false;
            LblHeader.Text = "View Unit";
            drpGroup.Visible = false;
            drpBranch.Visible = false;
            txtCode.Visible = false;
            txtName.Visible = false;
            txtEffectiveFrom.Visible = false;
            txtEffectiveTo.Visible = false;
            Image1.Visible = false;
            Image2.Visible = false;

            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            //unitid_v
            //Page.Request.QueryString[unitid_v];
            string unitStr = Page.Request.Params["unitid_v"].ToString();
            Session["unitId"] = unitStr;
            
            int unitID = int.Parse(unitStr);
            SalesOneUnit unt = new SalesOneUnit();
            SalesOneGroup grp = new SalesOneGroup();
            Branch br = new Branch();

            StructureFacade sf = new StructureFacade();
            unt = sf.GetUnit(unitID);
            br = sf.GetBranch(unt.BranchId);
            int groupID = br.GroupId;
            grp = sf.GetGroup(groupID);

            lblGroup.Text = grp.Name;
            labelBranch.Text = br.BranchName;
            FieldLblCode.Text = unt.Code;
            FieldLblName.Text = unt.Name;
            FieldLblFrom.Text = unt.From;
            FieldLblTo.Text = unt.To;
            //Session.Remove("unitId");
            //Session.Remove("branchId");

        }

        public void Update()
        {
            LblHeader.Text = "Update Unit";
            BtnDone.Text = "Update";
            FillDropDown("Group");
            FillDropDown("BranchOrg");
            //unitid_u
            //branchid_u
            string branchstr = Page.Request.Params["branchid_u"].ToString();
            int branchID = int.Parse(branchstr);

            string unitStr = Page.Request.Params["unitid_u"].ToString();
            Session["unitId"] = unitStr;
            int unitID = int.Parse(unitStr);

            StructureFacade unitFecade = new StructureFacade();
            SalesOneUnit sou = new SalesOneUnit();
            SalesOneGroup sog = new SalesOneGroup();
            Branch br = new Branch();
            br = unitFecade.GetBranch(branchID);
            int groupid = br.GroupId;

            sou = unitFecade.GetUnit(unitID);
            sog = unitFecade.GetGroup(groupid);

            //sog=unitFecade.GetGroup(unitFecade.GetGroups());
            drpGroup.SelectedValue = sog.GroupId.ToString();
            // drpGroup.Text = sog.Name;
            drpBranch.SelectedValue = br.BranchId.ToString();
            // drpBranch.Text = unitFecade.GetBranch(branchID).BranchName;


            //drpGroup.Text=unitFecade.GetGroup(int.Parse(drpGroup.SelectedValue))
            txtCode.Text = sou.Code;
            txtName.Text = sou.Name;
            txtEffectiveFrom.Text = sou.From;
            txtEffectiveTo.Text = sou.To;


        }



        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("unitId");
            Session.Remove("BID");
            Response.Redirect("~/Structure/ManageUnits.aspx");
        }

      





    }
}

